
'---------------------------------------------------
' Hands-On 5-1
'---------------------------------------------------

Public Function SumItUp(m, n)
    SumItUp = m + n
End Function


'---------------------------------------------------
' Hands-On 5-2
' No code in this Hands-On.
' Please follow the instructions in the book.
'---------------------------------------------------


'---------------------------------------------------
' Hands-On 5-3
'---------------------------------------------------

Sub RunSumItUp()
    Dim m As Single, n As Single
    m = 370000
    n = 3459.77

    Debug.Print SumItUp(m, n)
    MsgBox "Open the Immediate Window to see the result."
End Sub


'---------------------------------------------------
' Hands-On 5-4
'---------------------------------------------------

Sub NumOfCharacters()
    Dim f As Integer
    Dim l As Integer

    f = Len(InputBox("Enter first name:"))
    l = Len(InputBox("Enter last name:"))

    MsgBox SumItUp(f, l)
End Sub


'---------------------------------------------------
' Hands-On 5-5
'---------------------------------------------------

Sub EnterText()
    Dim m As String, n As String, r As String

    m = InputBox("Enter your first name:")
    n = InputBox("Enter your last name:")
    r = JoinText(m, n)

    MsgBox r
End Sub


Function JoinText(k, o)
    JoinText = k + " " + o
End Function


Function NumOfDays()
    NumOfDays = 7
End Function


Sub DaysInAWeek()
    MsgBox "There are " & NumOfDays & " days in a week."
End Sub


'---------------------------------------------------
' Hands-On 5-6
'---------------------------------------------------

Sub HowMuch()
    Dim num1 As Single
    Dim num2 As Single
    Dim result As Single

    num1 = 45.33
    num2 = 19.24


    result = MultiplyIt(num1, num2)
    MsgBox result
End Sub


Function MultiplyIt(num1, num2) As Integer
    MultiplyIt = num1 * num2
End Function


'---------------------------------------------------
' Hands-On 5-7
'---------------------------------------------------

Sub ThreeNumbers()
    Dim num1 As Integer, num2 As Integer, num3 As Integer
    num1 = 10
    num2 = 20
    num3 = 30

    MsgBox MyAverage(num1, num2, num3)
    MsgBox num1
    MsgBox num2
    MsgBox num3
End Sub


Function MyAverage(ByVal num1, ByVal num2, ByVal num3)
    num1 = num1 + 1

    MyAverage = (num1 + num2 + num3) / 3
End Function


'---------------------------------------------------
' Hands-On 5-8
'---------------------------------------------------

Function Avg(num1, num2, Optional num3)
    Dim totalNums As Integer

    totalNums = 3

    If IsMissing(num3) Then
        num3 = 0
        totalNums = totalNums - 1
    End If

    Avg = (num1 + num2 + num3) / totalNums
End Function


'---------------------------------------------------
' Hands-On 5-9
' Statements to be entered in the Immediate Window
'---------------------------------------------------

age = 18
birthdate = #1/1/1981#
firstName = "John"
?VarType(age)
?VarType(birthdate)
?VarType(firstName)


'---------------------------------------------------
' Hands-On 5-10
' Statements to be entered in the Immediate Window
' Each statement must be entered on one line
'---------------------------------------------------

MsgBox "All done. Now open your workbook file and place an empty disk in the diskette drive. The following procedure will copy this file to the disk."

MsgBox "All done." & Chr(13) & "Now open your workbook file and place" & Chr(13) & "an empty disk in the diskette drive." & Chr(13) & "The following procedure will copy this file to the disk."

' Code in Hands-On 5-10

Sub MyMessage()
    MsgBox "All done." & Chr(13) _
       & "Now open your workbook file and place" & Chr(13) _
       & "an empty disk in the diskette drive." & Chr(13) _
       & "The following procedure will copy this file to the disk."
End Sub


Sub MyMessage2()
    MsgBox "All done." & Chr(10) & Chr(10) _
       & "Now open your workbook file and place" & Chr(13) _
       & "an empty disk in the diskette drive." & Chr(13) & Chr(13) _
       & "The following procedure will copy this file to the disk."
End Sub

'---------------------------------------------------
' Hands-On 5-11
' Statements to be entered in the Immediate Window
'---------------------------------------------------

MsgBox "Do you want to proceed?", 292
MsgBox "Do you want to proceed?", vbYesNo + vbQuestion + vbDefaultButton2


'---------------------------------------------------
' Hands-On 5-12
'---------------------------------------------------

Sub MsgYesNo()
    Dim question As String
    Dim myButtons As Integer

    question = "Do you want to open a new workbook?"
    myButtons = vbYesNo + vbQuestion + vbDefaultButton2

    MsgBox question, myButtons
End Sub


Sub MsgYesNo2()
    Dim question As String
    Dim myButtons As Integer
    Dim myTitle As String

    question = "Do you want to open a new workbook?"
    myButtons = vbYesNo + vbQuestion + vbDefaultButton2
    myTitle = "New workbook"

    MsgBox question, myButtons, myTitle
End Sub


'---------------------------------------------------
' Hands-On 5-13
'---------------------------------------------------

Sub MsgYesNo3()
	Dim question As String
	Dim myButtons As Integer
	Dim myTitle As String

	Dim myChoice As Integer

	question = "Do you want to open a new workbook?"
	myButtons = vbYesNo + vbQuestion + vbDefaultButton2
	myTitle = "New workbook"
	myChoice = MsgBox(question, myButtons, myTitle)

	MsgBox myChoice
End Sub


'---------------------------------------------------
' Hands-On 5-14
'---------------------------------------------------

Sub Informant()
	InputBox prompt:="Enter your place of birth:" & Chr(13) _
	   & " (e.g., Boston, Great Falls, etc.) "
End Sub


Sub Informant2()
    Dim myPrompt As String
    Dim town As String

    Const myTitle = "Enter data"

    myPrompt = "Enter your place of birth:" & Chr(13) _
       & "(e.g., Boston, Great Falls, etc.)"
    town = InputBox(myPrompt, myTitle)

    MsgBox "You were born in " & town & ".", , "Your response"
End Sub


'---------------------------------------------------
' Hands-On 5-15
'---------------------------------------------------

Sub AddTwoNums()
    Dim myPrompt As String
    Dim value1 As String
    Dim mySum As Single

    Const myTitle = "Enter data"

    myPrompt = "Enter a number:"
    value1 = InputBox(myPrompt, myTitle, 0)
    mySum = value1 + 2

    MsgBox mySum & " (" & value1 & " + 2)"
End Sub


'---------------------------------------------------
' Hands-On 5-16
'---------------------------------------------------

Sub WhatRange()
    Dim newRange As Range
    Dim tellMe As String
    tellMe = "Use the mouse to select a range:"
    Set newRange = Application.InputBox(prompt:=tellMe, _
        Title:="Range to format", _
        Type:=8)
    newRange.NumberFormat = "0.00"
    newRange.Select
End Sub


Sub WhatRange2()
    Dim newRange As Range
    Dim tellMe As String

    On Error GoTo VeryEnd

    tellMe = "Use the mouse to select a range:"
    Set newRange = Application.InputBox(prompt:=tellMe, _
        Title:="Range to format", _
        Type:=8)
    newRange.NumberFormat = "0.00"
    newRange.Select

VeryEnd:
End Sub


'---------------------------------------------------
' Hands-On 5-17
'---------------------------------------------------

Sub AboutUserMaster()
    Dim first As String, last As String, full As String

    Call GetUserName(full)

    first = GetFirst(full)
    last = GetLast(full)
    Call DisplayLastFirst(first, last)
End Sub



Sub GetUserName(fullName As String)
    fullName = InputBox("Enter first and last name:")
End Sub


Function GetFirst(fullName As String)
    Dim space As Integer
    
    space = InStr(fullName, " ")

    GetFirst = Left(fullName, space - 1)
End Function


Function GetLast(fullName As String)
    Dim space As Integer

    space = InStr(fullName, " ")

    GetLast = Right(fullName, Len(fullName) - space)
End Function


Sub DisplayLastFirst(firstName As String, lastName As String)
    MsgBox lastName & ", " & firstName
End Sub





